#include "Ship.h"
#include <Ogre.h>
#include <OgreMesh.h>
#include <string>
#include "BHstring.h"

using namespace Ogre;

Ship::Ship(){}

Ship::Ship(ship_type _type, std::string _name ,Vector3 _original_pos, Quaternion _original_ori,bool _init_graphic):mType(_type)
{
	std::string mesh;
	Thruster* t;
	Battery* b;

	switch (_type)
	{
	case VIPER:
		mesh = "viper.mesh";
		t = new Thruster();
		t->mPosition = Vector3(0.75,1.25,-4);
		mThrusters.push_back(t);
		t = new Thruster();
		t->mPosition = Vector3(-0.75,1.25,-4);
		mThrusters.push_back(t);
		t = new Thruster();
		t->mPosition = Vector3(0,1.75,-4);
		mThrusters.push_back(t);
		mDimensions = Vector2(12.0,12.0f);
		b = new Battery();
		b->mPosition = Vector3(1.05,0.85,1);
		mBatteries.push_back(b);
		b = new Battery();
		b->mPosition = Vector3(-1.05,0.85,1);
		mBatteries.push_back(b);
		break;
	case RAIDER:
		mesh = "cylonraider.mesh";
		t = new Thruster();
		t->mPosition = Vector3(0.5,-0.75,-4);
		mThrusters.push_back(t);
		t = new Thruster();
		t->mPosition = Vector3(-0.5,-0.75,-4);
		mThrusters.push_back(t);
		t = new Thruster();
		t->mPosition = Vector3(0,-0.6,-4);
		mThrusters.push_back(t);
		mDimensions = Vector2(5.0f,5.0f);
		b = new Battery();
		b->mPosition = Vector3(1.15,-0.85,-0.25);
		mBatteries.push_back(b);
		b = new Battery();
		b->mPosition = Vector3(-1.15,-0.85,-0.25);
		mBatteries.push_back(b);
		break;
	case GALACTICA:
		mesh = "galactica.mesh";
		break;
	}

	mPosition = _original_pos;
	mOrientation = _original_ori;

	if (_init_graphic) init_graphic(_name);

}

void Ship::init_graphic(std::string& _name)
{
	std::string mesh;
	switch (mType)
	{
	case VIPER:
		mesh = "viper.mesh";
		break;
	case RAIDER:
		mesh = "cylonraider.mesh";
		break;
	case GALACTICA:
		mesh = "galactica.mesh";
		break;
	}

	SceneManager* mngr = Root::getSingleton().getSceneManager("SceneManager") ;
	mNode = mngr->getRootSceneNode()->createChildSceneNode(_name);
	Entity* ship_entity = mngr->createEntity(_name,mesh);
	mNode->attachObject(ship_entity);		
	mNode->scale(10,10,10);	
	mNode->setPosition(mPosition);
	mNode->setOrientation(mOrientation);
	
	if (mThrusters.size() == 0 ) return;

	// create a particle system with 200 quota, then set its material and dimensions
    ParticleSystem* thrusters = mngr->createParticleSystem(200);
    thrusters ->setMaterialName("Examples/Flare");
	thrusters->setDefaultDimensions(mDimensions.x,mDimensions.y);
	// attach our thruster particles to the rear of the ship       	
	mNode->createChildSceneNode()->attachObject(thrusters);
	
	// create two emitters for our thruster particle system
	for (unsigned int i = 0; i < mThrusters.size(); i++)
	{
		ParticleEmitter* emitter = thrusters ->addEmitter("Point");  // add a point emitter		
		// set the emitter properties
		emitter->setAngle(Degree(3));
		emitter->setTimeToLive(0.5);
		emitter->setEmissionRate(25);
		emitter->setParticleVelocity(25);
		emitter->setDirection(Vector3::NEGATIVE_UNIT_Z);
		emitter->setColour(ColourValue::White, ColourValue::Red);
		emitter->setPosition(mThrusters[i]->mPosition);			
	}	

	/*Entity* thisEntity = mngr->createEntity(_name + "battery", "Line"); 
	thisEntity->setMaterialName("Example/BeachStones"); 
	SceneNode* thisSceneNode = mngr->getRootSceneNode()->createChildSceneNode(); 
	thisSceneNode->setPosition(0, 0, 0); 
	//thisSceneNode->scale(10,100,100);
	thisSceneNode->attachObject(thisEntity);*/

	//creation of battery shots
	/*ManualObject* battery_shot;
	for (unsigned int i = 0 ; i < mBatteries.size(); i++)
	{
		Vector3 pos_origin = mBatteries[i]->mPosition;
		Vector3 pos_end = pos_origin + Vector3(0,0,100) ;
		battery_shot = mngr->createManualObject(); 		
		battery_shot->begin("BaseWhiteNoLighting", RenderOperation::OT_LINE_STRIP); 
		battery_shot->position(pos_origin);
		battery_shot->position(pos_end);		
		battery_shot->end();
		battery_shot->setVisible(false);
		mNode->attachObject(battery_shot);
		mBatteryShots.push_back(battery_shot);
	}*/



}

Ship::~Ship()
{
	for( unsigned int i = 0 ; i < mThrusters.size() ; i++)
	{
		delete mThrusters[i];
	}
}



void Ship::update(Ogre::Vector3 _position, Ogre::Quaternion _orientation)
{	
	mNode->setPosition(_position);
	mNode->setOrientation(_orientation);
	mPosition = _position;
	mOrientation = _orientation;
	if (mType == RAIDER) mNode->translate(0,5,0,SceneNode::TS_LOCAL);
	if (mType == VIPER) mNode->translate(0,-20,0,SceneNode::TS_LOCAL);
}

void Ship::shoot(int _id)
{
	if (mBatteryShots.size() > _id) mBatteryShots[_id]->setVisible(true);
}

void Ship::un_shoot(int _id)
{
	if (mBatteryShots.size() > _id) mBatteryShots[_id]->setVisible(false);
}

void Ship::respawn()
{
	mNode->setVisible(false);
}